package com.example.project.controller;


import com.example.project.common.MyTool;
import com.example.project.dto.SafetyTrainBinDTO;
import com.example.project.dto.SafetyTrainPageDTO;
import com.example.project.entity.SafetyTrain;
import com.example.project.service.SafetyTrainService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

/**
 * (SafetyTrain)表控制层
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@RestController
@RequestMapping("safetyTrain")
public class SafetyTrainController {
    /**
     * 服务对象
     */
    @Resource
    private SafetyTrainService safetyTrainService;

    /**
     * 分页查询
     *
     * param safetyTrain 筛选条件
     * param pageRequest      分页对象
     * @return 查询结果
     */
    @CrossOrigin
    @PostMapping("findPage")
    public ResponseEntity<Page<SafetyTrain>> queryByPage(@RequestBody SafetyTrainPageDTO data) throws ParseException {
        MyTool myTool = new MyTool();
        com.example.project.entity.Page page = data.getPage();
        Date end=null;
        Date star=null;
        if(data.getMyTime2().getEndTime()!=null&&data.getMyTime2().getEndTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            end=format.parse(data.getMyTime2().getEndTime()+":00");
        }
        if(data.getMyTime2().getStartTime()!=null&&data.getMyTime2().getStartTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            star =format.parse(data.getMyTime2().getStartTime()+":00");

        }
        //培训日期的上限
        Date day = data.getTrainDate();
        //培训日期的下限
        Date nextDay = myTool.nextDay(day);

        return ResponseEntity.ok(this.safetyTrainService.queryByPage(data.getSafetyTrain(), PageRequest.of(page.getPage(),page.getSize()), star, end ,day, nextDay));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @CrossOrigin
    @PostMapping("{id}")
    public ResponseEntity<SafetyTrain> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.safetyTrainService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param safetyTrain 实体
     * @return 新增结果
     */@CrossOrigin
    @PostMapping("add")
    public ResponseEntity<SafetyTrain> add(@RequestBody SafetyTrain safetyTrain) {
        return ResponseEntity.ok(this.safetyTrainService.insert1(safetyTrain));
    }

    /**
     * 编辑数据
     *
     * @param safetyTrain 实体
     * @return 编辑结果
     */
    @CrossOrigin
    @PutMapping("put")
    public ResponseEntity<SafetyTrain> edit(@RequestBody SafetyTrain safetyTrain) {
        return ResponseEntity.ok(this.safetyTrainService.update1(safetyTrain));
    }

    /**
     * 删除数据
     *
     * param id 主键
     * @return 删除是否成功
     */
    @CrossOrigin
    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(@RequestBody SafetyTrain data) {
        return ResponseEntity.ok(this.safetyTrainService.deleteById1(data.getSafetyTrainId()));
    }
    /**
     * 删除数据
     *
     * param id 主键
     * @return 删除是否成功
     */
    @CrossOrigin
    @DeleteMapping("delIds")
    public ResponseEntity<Boolean> deleteByIds(@RequestBody SafetyTrainBinDTO data) {
        return ResponseEntity.ok(this.safetyTrainService.deleteByIds(data.getIds()));
    }

}

